package com.gopay.bis.service.bank.impl.wap;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.Wap.GdbWapB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.GdbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.gdb.ebank.GdbB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayInteractRemoteService;
/**
 * 广发银行wapB2C业务实现
 * @author lilili
 *
 */
public class GdbWapPayB2CBankServiceImpl implements WapPayInteractRemoteService {

	@Resource(name="gdbWapB2cManager")
	private GdbWapB2cManager gdbWapB2cManager;
	
	@Override
	public Result pay(BaseMessage base) {
		 Result result = new Result(false);
	        try {
	            GdbB2cMessage messageB2C = (GdbB2cMessage) gdbWapB2cManager.sign(base);
	            result.setDefaultModel(messageB2C);
	            result.setSuccess(true);
	        } catch (GopayException e) {
	            BankLogUtils.printServiceImplExceptionLog(e);
	            result.setResultCode(e.getErrCode());
	        } catch (Exception ex) {
	            BankLogUtils.printServiceImplExceptionLog(ex);
	            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
	        }
	        return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		 Result result = new Result(false);
	        try {
	            GdbB2cMessage messageB2C = new GdbB2cMessage();
	            BeanUtils.copyProperties(base, messageB2C);
	            boolean flag = (Boolean) gdbWapB2cManager.verify(messageB2C);
	            result.setModel("message", messageB2C);
	            result.setDefaultModel(flag);
	            result.setSuccess(true);
	            if (flag) {
	                // 验签成功
	                Map<String, String> param = new HashMap<String, String>();
	                if (GdbConstants.PAY_STATUS_SUCCESS.equals(messageB2C.getRetCode())) {
	                    // 支付成功入账
	                    param.put("orderId", messageB2C.getOrderId());
	                    param.put("amt", messageB2C.getAmount().toString());
	                    param.put("cardType", "Unknown");
	                    param.put("notifyNum", "1");
	                    param.put("payStatus", "00");
	                    param.put("retCode", "00");
	                    param.put("retMsg", "验签成功");
	                    param.put("signData", messageB2C.getSignMessage());
	                    param.put("isFront", base.isFront() + "");
	                    result.setModel("isNotifyCashier", param);
	                    result.setOrderStatus(true);
	                } else {
	                    // 通知后台地址该订单失败
	                    param.put("orderId", messageB2C.getOrderId());
	                    param.put("amt", messageB2C.getAmount().toString());
	                    param.put("cardType", "Unknown");
	                    param.put("notifyNum", "1");
	                    param.put("payStatus", "01");
	                    param.put("retCode", "01");
	                    param.put("retMsg", "验签失败");
	                    param.put("signData", messageB2C.getSignMessage());
	                    param.put("isFront", base.isFront() + "");
	                    result.setModel("isNotifyCashier", param);
	                    result.setOrderStatus(false);
	                }
	            }
	        } catch (GopayException e) {
	            BankLogUtils.printServiceImplExceptionLog(e);
	            result.setResultCode(e.getErrCode());
	        } catch (Exception ex) {
	            BankLogUtils.printServiceImplExceptionLog(ex);
	            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6030);
	        }

	        return result;
	}


	@Override
	public Result query(BaseMessage base) {
		 Result result = new Result(false);
	        try {
	            GdbB2cMessage messageB2C = new GdbB2cMessage();
	            BeanUtils.copyProperties(base, messageB2C);
	            GdbB2cMessage queryB2C = (GdbB2cMessage) gdbWapB2cManager.query(messageB2C);
	            result.setDefaultModel(queryB2C);
	            result.setSuccess(true);
	            if (BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())) {
	                Map<String, String> param = new HashMap<String, String>();
	                param.put("orderId", messageB2C.getOrderId());
	                param.put("amount", messageB2C.getAmount().toString());
	                if (queryB2C == null) {
	                    // 查询订单为空
	                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
	                    param.put("returnMessage", "银行返回对账结果为空");
	                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
	                    result.setModel("queryResult", param);
	                    return result;
	                }

	                if (!(GdbConstants.QUERY_STATUS_SUCCESS.equals(queryB2C.getOrderStatus()))) {
	                    // 查询订单失败
	                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
	                    param.put("returnMessage", "银行订单查询失败" + queryB2C.getRetCode());
	                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
	                } else {
	                    // 查询成功
	                    // 成功处理
	                    if (GdbConstants.PAY_STATUS_SUCCESS.equalsIgnoreCase(queryB2C.getRetCode())) {
	                        param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
	                        param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
	                        param.put("returnMessage", "交易成功");
	                    }
	                    // 失败处理
	                    else if (!(GdbConstants.PAY_STATUS_SUCCESS.equalsIgnoreCase(queryB2C.getRetCode()))) {
	                        param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
	                        param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
	                        param.put("returnMessage", "订单处理失败");
	                    }
	                }
	                result.setModel("queryResult", param);
	            }
	        } catch (GopayException e) {
	            BankLogUtils.printServiceImplExceptionLog(e);
	            result.setResultCode(e.getErrCode());
	        } catch (Exception ex) {
	            BankLogUtils.printServiceImplExceptionLog(ex);
	            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
	        }
	        return result;
	}
	
	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
